package vincent.com.layouttest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.widget.ImageView;

import vincent.com.layouttest.util.LogUtil;

/**
 * Created by zhengwenchen on 2016/4/22.
 */
public class RotateImageView extends ImageView {

    private final String ROTATE_TAG = "czw_rotateview";
    private final String DRAWING_TAG = "czw_drawing_thread";

    private float mDegree = 0f;
    private float mPx = -1f;
    private float mPy = -1f;

    private boolean mIsPause = true;

    private DrawingThread mThread;

    public RotateImageView(Context context) {
        this(context, null);
    }

    public RotateImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RotateImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public boolean isPaused(){
        return mIsPause;
    }

    public void setPause(boolean isPause){
        this.mIsPause = isPause;
        ViewCompat.postInvalidateOnAnimation(this);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mPx = w/2;
        mPy = h/2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mDegree = mDegree % 360;
        canvas.rotate(mDegree, mPx, mPy);

        super.onDraw(canvas);
    }

    public void setDegree(float degree){
        this.mDegree = degree;
    }

    public float getDegree(){
        return this.mDegree;
    }

    public void reset(){
        mIsPause = true;
        mDegree = 0f;
        ViewCompat.postInvalidateOnAnimation(this);
        stop();
    }

    public void cancel(){
        mIsPause = true;
        mDegree = 0f;
        ViewCompat.postInvalidateOnAnimation(this);
        stop();
    }

    private class DrawingThread extends Thread{
        @Override
        public void run() {

            while(!mIsPause){

                mDegree += 0.306f;
                try{
                    ViewCompat.postInvalidateOnAnimation(RotateImageView.this);
                    Thread.sleep(17);
                }catch (Exception e){
                    LogUtil.e("czw_exception", e.getMessage());
                }
            }

            LogUtil.i(DRAWING_TAG, "drawing thread terminate");
        }
    }

    public void start(){
        LogUtil.i(ROTATE_TAG, "start");
        if(mThread != null && mThread.isAlive()){
            LogUtil.i(DRAWING_TAG, "drawing thread has already start and alive");
            return;
        }

        mIsPause = false;
        LogUtil.i(DRAWING_TAG, "make a new instance of drawing thread");
        mThread = new DrawingThread();

        mThread.start();
    }

    public void stop(){
        LogUtil.i(ROTATE_TAG, "stop");
        if(mThread != null && mThread.isAlive()){
            LogUtil.i(DRAWING_TAG, "drawing thread is alive");
            mThread.interrupt();
        }
        mIsPause = true;
    }
}
